package com.zhoug.zmui.swipe;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.zhoug.common.utils.LogUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 仿QQ向左侧滑菜单的List容器SwipeRecyclerView
 *
 * @Author 35574
 * @Date 2020/8/25
 * @Description
 */
public class ZMUISwipeRecyclerView extends RecyclerView {
    private static final String TAG = ">>>ZMUISwipeRecyclerView";
    private static boolean debug = false;
    private int mLastTouchViewPosition = -1;//上次触摸的ItemView 位置
    private ZMUISwipeMenuLayout mLastView;//上次触摸的ItemView
    private boolean openSwipe = false;
    private boolean closeChild = false;


    public static void setDebug(boolean debug) {
        ZMUISwipeRecyclerView.debug = debug;
    }

    public ZMUISwipeRecyclerView(@NonNull Context context) {
        super(context);
    }

    public ZMUISwipeRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ZMUISwipeRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOpenSwipe(boolean openSwipe) {
        this.openSwipe = openSwipe;
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent e) {
        if (!openSwipe) {
            return super.dispatchTouchEvent(e);
        }
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                closeChild = false;
                if (debug) {
                    LogUtils.d(TAG, "dispatchTouchEvent:ACTION_DOWN");
                }
                //获取坐标位置的itemView
                View childView = findChildViewUnder(e.getX(), e.getY());
                if (childView != null) {
                    if (debug) {
                        LogUtils.d(TAG, "abc:childView=" + childView.hashCode());

                    }
                    //获取坐标位置的itemView
                    int position = getChildAdapterPosition(childView);
                    ZMUISwipeMenuLayout lastView = mLastView;
                    int lastTouchViewPosition = mLastTouchViewPosition;
                    if (childView instanceof ZMUISwipeMenuLayout) {
                        mLastView = (ZMUISwipeMenuLayout) childView;
                        mLastTouchViewPosition = position;
                    }
                    //不是上次按下时的Item
                    if (position != lastTouchViewPosition && lastView != null) {
                        if (debug) {
                            LogUtils.d(TAG, "abc:不是上次按下时的Item");

                        }
                        if (lastView.isMenuOpen()) {
                            //关闭
                            lastView.smoothCloseMenu();
                            closeChild = true;
                        }
                    }
                }
                if (debug) {
                    LogUtils.d(TAG, "abc:mLastView=" + (mLastView != null ? mLastView.hashCode() : ""));

                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                closeChild = false;
                break;
        }
       /* if(closeChild){
            return true;
        }*/
        return super.dispatchTouchEvent(e);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if (debug) {
            LogUtils.d(TAG, "abc:onTouchEvent mLastView=" + (mLastView != null ? (mLastView.hashCode()+",open="+mLastView.isMenuOpen()) : ""));
        }
        if (!openSwipe) {
            return super.onTouchEvent(e);
        }
      /*  if (mLastView != null && mLastView.isMenuOpen()) {
            LogUtils.d(TAG, "abc:false");
            return false;
        }*/
        return super.onTouchEvent(e);
    }
}
